﻿using System;
using Npgsql;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Drawing;

namespace posmoon.Tree
{
    public class HostTreeNode : TreeNode
    {
        #region attributes

        private string _name;
        private string _hostIP;
        private string _port;
        private string _userName;
        private string _userPassword;
        private string _defdb;
        private bool _connected;

        private NpgsqlConnection _conn;
        private EncryptDecrypt _secure;

        private RegistryKey _key;

        #endregion

        public HostTreeNode(string name, string host, string defdb, string port ,string user, string pass) : base(name + "("+host+")")
        {
            _name = name;
            _hostIP = host;
            _defdb = defdb;
            _port = port;
            _userName = user;
            _userPassword = pass;
            _secure = new EncryptDecrypt();
            _connected = false;
            this.ForeColor = SystemColors.GrayText;
            
            InitializeConnection();
        }

        public HostTreeNode(RegistryKey key)
        {
            _secure = new EncryptDecrypt();
            _key = key;
            _connected = false;
            this.ForeColor = SystemColors.GrayText;
        }

        public string HostName
        {
            get { return _name; }
            set { _name = value; }
        }

        public string HostIP
        {
            get { return _hostIP; }
        }

        public bool Connected
        {
            get { return _connected; }
        }

        public string HostPort
        {
            get { return _port; }
        }

        private bool SaveToRegistry()
        {
            new RegistrytSaver().WriteValue(_name, _secure.GetEncrypted(_hostIP), 
                                            _secure.GetEncrypted(_defdb),
                                            _secure.GetEncrypted(_port), 
                                            _secure.GetEncrypted(_userName), 
                                            _secure.GetEncrypted(_userPassword));
            return true;
        }

        public bool Register()
        {
            try
            {
                _conn.Open();
                _conn.Close();

                try
                {
                    SaveToRegistry();
                }
                catch
                {
                    throw new Exception("Error saving!!!");
                }

                return true;
            }
            catch
            {
                return false;
            }            
        }

        public void ReadFromRegistry()
        {
            new RegistrytSaver(_key).ReadValues(ref _hostIP, ref _defdb, ref _port, ref _userName, ref _userPassword);

            _hostIP = _secure.GetDecrypted(_hostIP);
            _defdb = _secure.GetDecrypted(_defdb);
            _port = _secure.GetDecrypted(_port);
            _userName = _secure.GetDecrypted(_userName);
            _userPassword = _secure.GetDecrypted(_userPassword);

            InitializeConnection();
        }

        private void InitializeConnection()
        {
            string connstr = "Server=" + _hostIP +
                 ";Port=" + _port +
                 ";User Id=" + _userName +
                 ";Password=" + _userPassword +
                 ";Database=" + _defdb + ";";

            _conn = new NpgsqlConnection(connstr);
        }

        public void openConnection()
        {
            try
            {
                _conn.Open();

                if (_conn.State == System.Data.ConnectionState.Open)
                    _connected = true;                
                else
                    _connected = false;
            }
            catch
            { }
        }

        public NpgsqlConnection Connection
        {
            get { return _conn; }
        }

        public string UserName
        {
            get { return _userName; }
        }

        public string UserPassword
        {
            get { return _userPassword; }
        }
    }
}
